package main;

import java.util.HashMap;
import java.util.Map;

/**
 * 
 * Main class.
 * 
 */
public class Main {

	/**
	 * Entry point of the program.
	 * 
	 * @param args
	 *            arguments should be filenames
	 */
	public static void main(String[] args) {

		// should we use parallel or sequential partitioner
		boolean useParallel = true;

		// if no arguments, then use this configuration.
		// TODO: remove this before deployment
		if (args.length < 1) {
			args = new String[] { "tests/EliaSand_generated_305.xml" };
			Console.setEnable(true);
			TimeTest.isEnabled = true;
		}

		TimeTest.start("program");

		// init pool-sizes according to processor number.
		Simulate.initPool();

		// graph representation
		Map<Integer, Node> mapGraph = new HashMap<Integer, Node>();

		// parser for the input
		Parser parser = new Parser();
		// ParserPar = new ParserPar();

		// TODO: do in parallel
		for (String filename : args) {

			if (parser.parseFile(filename, mapGraph)) {
				mapGraph = parser.getGraph();
			} else {
				System.exit(-1);
			}
		}

		Partitioner partitioner = new Partitioner();

		// create initial partition (sequential)
		TimeTest.start("create initial partition");
		Partition partition = partitioner.createInitialPartitionSeq(mapGraph);
		Console.println("Initial partition size: " + partition.size());
		Console.println(partition.toString());
		TimeTest.stop();

		// partition
		TimeTest.start("partition");
		if (useParallel)
			partitioner.partitionPar(partition);
		else
			partitioner.partitionSeq(partition);
		Console.println("Partition size: " + partition.size());
		Console.println(partition.toString());
		TimeTest.stop();

		TimeTest.stop();
		System.exit(partition.size());
	}

}
